﻿
(function ($) {

    $.extend({
        Ajax: {
            defaults: {
                targetId: null,
                loading: true,
                data: null,
                resultMode: null,
                hashPwd: null,
                onRequest: null,
                onComplete: null,
                onError: null,
                method: 'POST',
                dataType: 'html'
            },

            submit: function (f, options) {

                var _f = (typeof f == 'object') ? f : $('#' + f);

                var _s = $.extend({}, this.defaults, options || {},$.metadata ? _f.metadata({type:'attr',name:'data-jquery-ajax'}):{});

                if (window.tinymce && _f.find('.tinymce')) {
                    window.tinyMCE.triggerSave();
                };

                /* if ($.validator && $.validator.unobtrusive)
                $.validator.unobtrusive.parse(_f);*/

                //, invalidHandler: function () { _f.find(":input.error:first").focus(); }
                _f.validate({ meta: 'validate' });

                if (_f.valid()) {

                    if (_s.loading && $.Loading) {
                        $.Loading.show();
                    };

                    if (_s.hashPwd && $.Hash) {
                        _f.find(":password").each(function () {
                            var _v = $(this).val();
                            if (_v.length > 0)
                                $(this).val($.Hash.sha256(_v));
                        });
                    };

                    _f.find(":checkbox").each(function () {
                        if (!$(this).attr('value'))
                            $(this).val($(this).is(":checked"));
                    });

                    if ($.isFunction(_s.onRequest)) {
                        _s.onRequest.apply();
                    }

                    $.ajax({
                        type: _s.method,
                        url: _f.attr("action"),
                        data: _f.serialize(),
                        cache: false,
                        statusCode: {
                            404: function () {
                                alert('page not found');
                            }
                        },
                        success: function (data) {
                            if ((_s.dataType == 'html' || _s.dataType == 'text') && _s.target) {

                                switch (_s.resultMode) {
                                    case 'append':
                                        $(_s.target).append(data);
                                        break;
                                    case 'replacewith':
                                        $(_s.target).replaceWith(data);
                                        break;
                                    case 'after':
                                        $(data).after($(_s.target));
                                        break;
                                    case 'before':
                                        $(data).before($(_s.target));
                                        break;
                                    default:
                                        $(_s.target).html(data);
                                        break;
                                }


                            }
                            if (_s.onComplete) {
                                if ($.isFunction(_s.onComplete))
                                    _s.onComplete.call(this, data);
                                else
                                    eval(_s.onComplete);
                            }

                            if (_s.hashPwd) {
                                _f.find("input:password").val('');
                            }

                            if (_s.loading && $.Loading) {
                                $.Loading.hide();
                            }
                        }
                    })
                .error(function (xhr, ajaxOptions, thrownError) {
                    if (_s.onError) {
                        if ($.isFunction(_s.onError))
                            _s.onError.call(this, xhr, ajaxOptions, thrownError);
                        else
                            eval(_s.onError);
                    }

                    else {
                        alert('Error:' + xhr.status + '\n' + thrownError);
                    }

                    if (_s.loading && $.Loading) {
                        $.Loading.hide();
                    }

                });


                } // end validate

            },

            load: function (u, o) {

                var _s = $.extend({}, this.defaults, o || {});

                if (_s.loading && $.Loading) {
                    $.Loading.show();
                };


              /*  $.load(u, _s.data, function(data, status, XMLHttpRequest) {

                    
                });
*/

                $.ajax({

                    type: 'GET',
                    url: u,
                  //  dataType: _s.dataType,
                    statusCode: {
                        404: function () {
                            alert('page not found');
                        }
                    },

                    success: function (data) {
                        if ((_s.dataType == 'html' || _s.dataType == 'text') && _s.target) {

                            switch (_s.resultMode) {
                                case 'append':
                                    $(_s.target).append(data);
                                    break;
                                case 'replacewith':
                                    $(_s.target).replaceWith(data);
                                    break;
                                case 'after':
                                    $(data).after($(_s.target));
                                    break;
                                case 'before':
                                    $(data).before($(_s.target));
                                    break;
                                default:
                                    $(_s.target).html(data);
                                    break;
                            }


                        }
                        if (_s.onComplete) {
                            if ($.isFunction(_s.onComplete))
                                _s.onComplete.call(this, data);
                            else
                                eval(_s.onComplete);
                        }

                        if (_s.loading && $.Loading) {
                            $.Loading.hide();
                        }

                        /* if (window.history && window.history.pushState) {
                        window.history.pushState(null, null, u);
                        }*/
                    }

                }).error(function (xhr, ajaxOptions, thrownError) {
                    if (_s.onError) {
                        if ($.isFunction(_s.onError))
                            _s.onError.call(this, xhr, ajaxOptions, thrownError);
                        else
                            eval(_s.onError);
                    }

                    else {
                        alert('Error:' + xhr.status + '\n' + thrownError);
                    }

                });
            } //end load

        }

    });

})(jQuery);



